package com.demo.rydemo.d_dic.controller;


import com.demo.rydemo.d_dic.entity.DDic;
import com.demo.rydemo.d_dic.service.IDDicService;
import com.demo.rydemo.result.AjaxResult;
import com.demo.rydemo.result.CoreController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ry
 * @since 2020-08-28
 */
@Api(tags = "字典相关接口")
@RestController
@RequestMapping("/d_dic/d-dic")
public class DDicController extends CoreController {

    @Autowired
    IDDicService idDicService;

    @Autowired
    AjaxResult ajaxResult;


    @ApiOperation("/添加字典")
    @PostMapping("/addDic")
    AjaxResult addDic(@RequestBody DDic dDic){
        if (StringUtils.isNotEmpty(dDic.getDicName())){
            dDic.setDicId(UUID.randomUUID().toString().replace("-",""))
                    .setCreateTime(new Date());
            Boolean flag = false;
            flag = idDicService.save(dDic);
            if(flag){
                return ajaxResult.success("添加成功");
            }
            return ajaxResult.error("添加失败，请联系管理员");
        }
        return ajaxResult.error("传输数据错误，请联系管理员");
    }


    @ApiOperation("展示所有字典数据")
    @GetMapping("/listDic")
    AjaxResult listDic(){
        List<DDic> list = idDicService.list();
        return ajaxResult.success(list);
    }

}
